import { Response } from 'express'
import { ApiResponse } from '../types'

export class ResponseHandler {
    static success<T>(res: Response, data: T, message = '操作成功') {
        const response: ApiResponse<T> = {
            code: 200,
            message,
            data
        }
        res.json(response)
    }

    static error(res: Response, message = '操作失败', code = 500) {
        const response: ApiResponse<null> = {
            code,
            message,
            data: null
        }
        res.status(code).json(response)
    }

    static unauthorized(res: Response, message = '未授权') {
        this.error(res, message, 401)
    }

    static forbidden(res: Response, message = '无权限') {
        this.error(res, message, 403)
    }

    static notFound(res: Response, message = '资源不存在') {
        this.error(res, message, 404)
    }
} 